#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
int n,m,a[110][11],f[110][1030],ans;char c;
int te(int x){
	--x;
	int cnt = 0;
	for (int i = 0;i < m;++i){
		cnt += a[x][m - i] * (1 << i);
	}
	return cnt;
}
signed main(){
	cin >> n >> m;
	for (int i = 1;i <= n;++i) {
		for (int j = 1;j <= m;++j) {
			cin >> c;
			// printf("'%c'\n",c);
			if (c == 'P') a[i][j] = 0;
			else a[i][j] = 1;
			// cout << a[i][j];
		}
		// cout << endl;
	}
	// for (int i = 1; i <= n;++i) {
		// printf("%lld\n",te(i + 1));
	// }
	for (int i = 2;i <= n + 1;++i){
		for (int x = 0;x < (1 << m);++x){
			if ((x & (x << 1)) || (x & (x << 2)) || (te(i) & x)) continue;
			int paobing = __builtin_popcount(x);
			f[i][x] = paobing;
			for (int y = 0;y < (1 << m);++y){
				if ((y & (y << 1)) || (y & (y << 2)) || (te(i - 1) & y) || (x & y)) continue;
				for (int z = 0;z < (1 << m);++z){
					if ((f[i - 1][y] - f[i - 2][z] != __builtin_popcount(y)) || (z & (z << 1)) || (z & (z << 2)) || (te(i - 2) & z) || (x & z) || (y & z)) continue;
					f[i][x] = max(f[i][x],f[i - 1][y] + paobing);
				}
			}
		}
	}
	// for (int i = 2;i <= n + 1;++i){
	// 	cout << i - 1 << " ";
	// 	for (int j = 0; j < (1 << m);++j){
	// 		printf("%05lld ",f[i][j]);
	// 	}
	// 	cout << endl;
	// }
	for (int i = 0; i <= (1 << m);++i) ans = max(ans,f[n + 1][i]);
	cout << ans;
	return 0;
}